package com.jiuan.retrofit.util

import java.security.Key
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import javax.crypto.Cipher

internal class Encryption(strKey: String) {

    private var encryptCipher: Cipher? = null
    private var decryptCipher: Cipher? = null

    init {
        val key: Key
        try {
            key = getKey(strKey.toByteArray())
            encryptCipher = Cipher.getInstance("DES")
            encryptCipher!!.init(Cipher.ENCRYPT_MODE, key)
            decryptCipher = Cipher.getInstance("DES")
            decryptCipher!!.init(Cipher.DECRYPT_MODE, key)
        } catch (e: Exception) {
            Logger.e(e)
        }

    }

    @Throws(Exception::class)
    fun encrypt(encryptionText: String): String {
        return byteArrayToHexStr(encrypt(encryptionText.toByteArray()))
    }

    @Throws(Exception::class)
    fun encrypt(byteArray: ByteArray): ByteArray {
        return encryptCipher!!.doFinal(byteArray)
    }

    @Throws(Exception::class)
    fun decrypt(cipherText: String): String {
        return String(decrypt(hexStrToByteArray(cipherText)))
    }

    @Throws(Exception::class)
    fun decrypt(byteArray: ByteArray): ByteArray {
        return decryptCipher!!.doFinal(byteArray)
    }

    @Throws(Exception::class)
    private fun getKey(arrBTmp: ByteArray): Key {
        val arrB = ByteArray(8)
        var i = 0
        while (i < arrBTmp.size && i < arrB.size) {
            arrB[i] = arrBTmp[i]
            i++
        }
        val key = javax.crypto.spec.SecretKeySpec(arrB, "DES")
        return key
    }

    companion object {

        @Throws(Exception::class)
        fun byteArrayToHexStr(byteArray: ByteArray): String {
            val len = byteArray.size
            val sb = StringBuffer(len * 2)
            for (i in 0..len - 1) {
                var intTmp = byteArray[i].toInt()
                while (intTmp < 0) {
                    intTmp += 256
                }
                if (intTmp < 16) {
                    sb.append("0")
                }
                sb.append(Integer.toString(intTmp, 16))
            }
            return sb.toString()
        }

        @Throws(Exception::class)
        fun hexStrToByteArray(hexString: String): ByteArray {
            val byteArrayIn = hexString.toByteArray()
            val iLen = byteArrayIn.size
            val byteArrayOut = ByteArray(iLen / 2)
            var i = 0
            while (i < iLen) {
                val strTmp = String(byteArrayIn, i, 2)
                byteArrayOut[i / 2] = Integer.parseInt(strTmp, 16).toByte()
                i += 2
            }
            return byteArrayOut
        }

        /**
         * Get the MD5 value of string.

         * @param content the target string.
         * *
         * @return the MD5 value.
         */
        fun getMD5ForString(content: String): String {
            val md5Buffer = StringBuilder()
            try {
                val digest = MessageDigest.getInstance("MD5")
                val tempBytes = digest.digest(content.toByteArray())
                var digital: Int
                for (i in tempBytes.indices) {
                    digital = tempBytes[i].toInt()
                    if (digital < 0) {
                        digital += 256
                    }
                    if (digital < 16) {
                        md5Buffer.append("0")
                    }
                    md5Buffer.append(Integer.toHexString(digital))
                }
            } catch (e: NoSuchAlgorithmException) {
                Logger.e(e)
            }

            return md5Buffer.toString()
        }
    }

}
